package com.tx.studentManager.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.common.format.util.DateUtil;
import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.model.Department;
import com.tx.studentManager.model.Page;
import com.tx.studentManager.qo.DepartmentQO;
import com.tx.studentManager.service.DepartmentService;
import com.tx.studentManager.utils.DWZJsonUtil;

/**
 * 
 * @描述 院系操作控制器
 * @since studentManager-admin 1.0
 * @version 1.0 2015-9-14 下午3:08:17
 * @author 此生唯晴
 */
@Controller
@RequestMapping(value = "/department")
public class DepartmentController {
	Logger logger=Logger.getLogger(DepartmentController.class);
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private SystemConfig systemConfig;
	
	@RequestMapping(value = "/list")
	public ModelAndView list(
			DepartmentQO qo,
			@RequestParam(required = false) String createDateStartStr,
			@RequestParam(required = false) String createDateEndStr,
			@RequestParam(required = false, value = "numPerPage") Integer pageSize,
			@RequestParam(required = false, value = "pageNum") Integer pageNo) {

		if (StringUtils.isNotBlank(createDateEndStr)) {
			qo.setCreateDateEnd(DateUtil.str2DateTime(createDateEndStr));
		}
		if (StringUtils.isNotBlank(createDateStartStr)) {
			qo.setCreateDateStart(DateUtil.str2DateTime(createDateStartStr));
		}
		pageSize = pageSize == null ? systemConfig.getQueryPageSize() : pageSize;
		pageNo = pageNo == null ? 1 : pageNo;
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setCondition(qo);
		page = departmentService.list(page);
		ModelAndView modelAndView = new ModelAndView("department/depList");
		modelAndView.addObject("page", page);
		return modelAndView;
	}

	@RequestMapping("/addDep")
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("department/depAdd");
		return modelAndView;
	}

	/**
	 * 
	 * @描述 添加院系，持久化数据
	 * @param department
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", produces = "text/html;charset=UTF-8")
	public String toAdd(@ModelAttribute Department department) {

		if (StringUtils.isNotBlank(department.getDepName())) {
			int depSize = departmentService.list(department).size();
			if (depSize > 0) {
				return DWZJsonUtil.simpleJsonStr(
						DWZJsonUtil.RESULT_FAILURE_CODE, "该院系已存在！", null, null);
			} else {
				department = departmentService.insert(department);
				if (department != null) {
					return DWZJsonUtil.simpleJsonStr(
							DWZJsonUtil.RESULT_SUCCESS_CODE, "操作成功！",
							DWZJsonUtil.OPER_CLOSE_CURRENT, "depManager");
				}
			}
		} else {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"院系名称不能为空！！！", null, null);
		}
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
				"操作失败！！！", null, null);
	}

	/**
	 * 
	 * @描述 修改院系信息
	 * @param depId
	 * @return
	 */
	@RequestMapping("/modify")
	public ModelAndView modify(String depId) {
		Department department = departmentService.findById(depId);
		ModelAndView modelAndView = new ModelAndView("department/depModify");
		modelAndView.addObject("department", department);
		return modelAndView;
	}

	/**
	 * 
	 * @描述 修改院系信息
	 * @param depName
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
	public String toModify(String depName, String depId) {

		if (StringUtils.isNotBlank(depName)) {
			Department department = departmentService.findById(depId);
			department.setDepName(depName);
			boolean operResult = departmentService.update(department);
			if (operResult) {
				return DWZJsonUtil.simpleJsonStr(
						DWZJsonUtil.RESULT_SUCCESS_CODE, "修改成功",
						DWZJsonUtil.OPER_CLOSE_CURRENT, "depManager");
			} else {
				return DWZJsonUtil.simpleJsonStr(
						DWZJsonUtil.RESULT_FAILURE_CODE, "操作失败，未知原因！", null,
						null);
			}

		} else {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"院系名称不能为空", null, null);
		}
	}

	/**
	 * 
	 * @描述 删除院系
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delDep", produces = "text/html;charset=UTF-8")
	public String delete(String depId) {

		if (StringUtils.isBlank(depId)) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"指定院系ID不能为空", null, null);
		} else {
			boolean opResult=false ;
			try {
				opResult=departmentService.delete(depId);
			} catch (DataIntegrityViolationException e) {
				logger.info("删除院系时，发现存在关联专业，不允许删除！");
				//e.printStackTrace();
				return DWZJsonUtil.simpleJsonStr(
						DWZJsonUtil.RESULT_FAILURE_CODE, "存在关联专业，不允许删除！", null, null);
			}
			if (opResult) {
				return DWZJsonUtil.simpleJsonStr(
						DWZJsonUtil.RESULT_SUCCESS_CODE, "删除成功！", null, null);
			}
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"操作失败！", null, null);
		}

	}
}
